<!DOCTYPE html>
<html>
<head>
    <title>AI Chat Interface - Payara</title>
    <style>
        body {
            background-image: linear-gradient(rgba(0,44,62), rgba(0,44,62,.8), rgba(0,44,62));
            color: #fff;
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .logo-container {
            text-align: center;
            margin: 20px 0;
        }
        .logo-container img {
            width: 50%;
            max-width: 400px;
        }
        .chat-container {
            background-color: rgba(255, 255, 255, 0.1);
            border-radius: 10px;
            padding: 20px;
            margin: 20px 0;
        }
        #chat-messages {
            height: 400px;
            overflow-y: auto;
            margin-bottom: 20px;
            padding: 10px;
            background-color: rgba(0, 0, 0, 0.2);
            border-radius: 5px;
        }
        .message {
            margin: 10px 0;
            padding: 10px;
            border-radius: 5px;
        }
        .user-message {
            background-color: rgba(0, 100, 150, 0.5);
            margin-left: 20px;
        }
        .ai-message {
            background-color: rgba(255, 198, 0, 0.5);
            margin-right: 20px;
        }
        .ai-message pre {
            background-color: #1e1e1e;
            border-radius: 5px;
            padding: 15px;
            margin: 10px 0;
            overflow-x: auto;
        }
        .ai-message code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            white-space: pre;
            color: #d4d4d4;
            display: block;
        }
        .ai-message h3 {
            color: #ffffff;
            border-bottom: 1px solid rgba(255, 255, 255, 0.2);
            padding-bottom: 8px;
            margin-top: 20px;
            margin-bottom: 10px;
        }
        .ai-message ul {
            padding-left: 20px;
            margin: 10px 0;
        }
        .ai-message li {
            margin: 8px 0;
            line-height: 1.5;
        }
        .ai-message strong {
            color: #ffd700;
            font-weight: bold;
        }
        .ai-message ol {
            padding-left: 20px;
            margin: 10px 0;
        }
        .ai-message code:not(pre code) {
            background-color: rgba(30, 30, 30, 0.5);
            padding: 2px 5px;
            border-radius: 3px;
            font-size: 0.9em;
            display: inline;
        }
        .ai-message .output {
            background-color: #2d2d2d;
            border-left: 4px solid #ffd700;
            padding: 10px 15px;
            margin: 10px 0;
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
        }
        .ai-message ul {
            padding-left: 0;
            margin: 15px 0;
            list-style: none;
        }
        .ai-message ul li {
            position: relative;
            padding-left: 25px;
            margin: 10px 0;
            line-height: 1.6;
        }
        .ai-message ul li::before {
            content: "•";
            color: #ffd700;
            font-size: 1.2em;
            position: absolute;
            left: 8px;
            top: 50%;
            transform: translateY(-50%);
        }
        .ai-message .endpoint-list li {
            background-color: rgba(30, 30, 30, 0.3);
            border-radius: 5px;
            padding: 8px 12px 8px 35px;
            margin: 8px 0;
        }
        .ai-message .endpoint-list li::before {
            left: 12px;
        }
        .ai-message .http-method {
            color: #ffd700;
            font-weight: bold;
            margin-right: 8px;
        }
        .ai-message .endpoint-path {
            color: #00a6e1;
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
        }
        .ai-message .endpoint-description {
            color: #ffffff;
            margin-left: 8px;
        }
        .input-container {
            display: flex;
            gap: 10px;
            margin-top: 20px;
        }
        select, input, button {
            padding: 10px;
            border: none;
            border-radius: 5px;
            background-color: rgba(255, 255, 255, 0.9);
        }
        input {
            flex-grow: 1;
        }
        button {
            background-color: #00a6e1;
            color: white;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #0085b3;
        }
        a {
            font-weight: bold;
            color: white;
        }
    </style>
</head>
<body>
<div class="logo-container">
    <a href="https://www.payara.fish/"><img src="images/payara-fish-logo.svg" alt="Payara Logo"></a>
</div>

<h1>AI Chat Interface</h1>

<div class="chat-container">
    <div id="chat-messages"></div>

    <div class="input-container">
        <select id="ai-provider">
            <option value="openai">OpenAI</option>
            <option value="gemini">Gemini</option>
            <option value="deepseek">DeepSeek</option>
        </select>
        <input type="text" id="user-input" placeholder="Type your message here..." />
        <button onclick="sendMessage()">Send</button>
    </div>
</div>

<script>
    function appendMessage(message, isUser) {
        const chatMessages = document.getElementById('chat-messages');
        const messageDiv = document.createElement('div');
        messageDiv.className = `message ${isUser ? 'user-message' : 'ai-message'}`;

        let formattedMessage = message.replace(/```(\w*)\n([\s\S]*?)```/g,
            (match, language, code) => `<pre><code class="language-${language}">${code.trim()}</code></pre>`
        );

        formattedMessage = formattedMessage.replace(/###\s+(.*)/g, '<h3>$1</h3>');

        formattedMessage = formattedMessage.replace(/^\*\s+(.+)$/gm, '<li>$1</li>');

        formattedMessage = formattedMessage.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');

        formattedMessage = formattedMessage.replace(/`([^`]+)`/g, '<code>$1</code>');

        formattedMessage = formattedMessage.replace(/<li>[\s\S]+?(?=<\/li>)/g,
            match => `<ul>${match}</ul>`
        );

        formattedMessage = formattedMessage.replace(/^\d+\.\s+(.+)$/gm, '<li>$1</li>');

        messageDiv.innerHTML = formattedMessage;
        chatMessages.appendChild(messageDiv);
        chatMessages.scrollTop = chatMessages.scrollHeight;
    }

    async function sendMessage() {
        const input = document.getElementById('user-input');
        const provider = document.getElementById('ai-provider').value;
        const message = input.value.trim();

        if (!message) return;

        appendMessage(message, true);
        input.value = '';

        const baseUrl = 'http://localhost:8080/api';
        const urls = {
            'openai': `${baseUrl}/openai/chat`,
            'gemini': `${baseUrl}/gemini/chat`,
            'deepseek': `${baseUrl}/deepseek/chat`
        };

        try {
            const response = await fetch(`${urls[provider]}?message=${encodeURIComponent(message)}`, {
                method: 'GET',
                headers: {
                    'Accept': 'text/plain'
                }
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const result = await response.text();
            appendMessage(result, false);
        } catch (error) {
            appendMessage(`Error: ${error.message || 'Could not get response from the AI service.'}`, false);
            console.error('Error:', error);
        }
    }

    // Allow sending message with Enter key
    document.getElementById('user-input').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            sendMessage();
        }
    });
</script>
</body>
</html>